package cn.kgc.crowdfundingadminspringboot2.service;

import cn.kgc.crowdfundingadminspringboot2.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadminspringboot2.mapper.AdminMapper;
import cn.kgc.crowdfundingadminspringboot2.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot2.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;


    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Admin login(String loginAcct, String userPswd) {
        Admin admin=null;
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        if (CrowdFundingUtils.collectionEffective(admins)){
            admin=admins.get(0);
            if (CrowdFundingUtils.md5(userPswd).equals(admin.getUserpswd())){
                return admin;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        List<Admin> list=null;
        if (CrowdFundingUtils.stringEffective(keyword)){
            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria1 = example.createCriteria();
            criteria1.andLoginacctLike("%"+keyword+"%");
            AdminExample.Criteria criteria2 = example.createCriteria();
            criteria2.andUsernameLike("%"+keyword+"%");
            AdminExample.Criteria criteria3 = example.createCriteria();
            criteria3.andEmailLike("%"+keyword+"%");

            example.or(criteria2);
            example.or(criteria3);

            list=adminMapper.selectByExample(example);
        }else{
            list=adminMapper.selectByExample(null);
        }
        return new PageInfo<>(list);
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);

        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd = admin.getUserpswd();
        userPswd = CrowdFundingUtils.md5(userPswd);
        admin.setUserpswd(userPswd);

        // 执行保存
        adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String userPswd = admin.getUserpswd();
            userPswd = CrowdFundingUtils.md5(userPswd);
            admin.setUserpswd(userPswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}
